package com.qidian.mes.machine.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.machine.mapper.MachineMaintainItemsMapper;
import com.qidian.mes.machine.service.MachineMaintainItemsService;
import com.qidian.mes.pojo.Ledger;
import com.qidian.mes.pojo.MaintainItems;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/18 10:49
 */
@Service
public class MachineMaintainItemsServiceImpl implements MachineMaintainItemsService {

    @Autowired
    private MachineMaintainItemsMapper machineMaintainItemsMapper;

    @Override
    public MESResult selectList(MaintainItems maintainItems, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<MaintainItems> maintainItemsList = machineMaintainItemsMapper.selectList(maintainItems);
        PageInfo<MaintainItems> pageInfo = new PageInfo<>(maintainItemsList);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addItems(MaintainItems maintainItems) {
        if (machineMaintainItemsMapper.selectByCode(maintainItems.getProjectCode())!=null)
            return MESResult.error();
        if (machineMaintainItemsMapper.addItem(maintainItems) == 1)
            return MESResult.success(maintainItems);
        return MESResult.error();
    }

    @Override
    public MESResult updateItems(MaintainItems maintainItems) {
        MaintainItems temp = machineMaintainItemsMapper.selectByCode(maintainItems.getProjectCode());
        if (temp != null && !temp.getProjectCode().equals(maintainItems.getProjectCode()))
            return MESResult.error();
        else
            machineMaintainItemsMapper.updateItems(maintainItems);
        return MESResult.success();
    }

    @Override
    public MESResult selectById(Long projectId) {
        return MESResult.success(machineMaintainItemsMapper.selectById(projectId));
    }

    @Override
    public MESResult updateStatus(Long projectId) {
        return MESResult.success(machineMaintainItemsMapper.updateItemStatus(projectId));
    }
}
